﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QFileDialog>
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QTime>

#include <QMediaPlayer>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);


    timer = new QTimer(this);
    timer->start(1000);


    ui->statusbar->setSizeGripEnabled(false);
    ui->statusbar->setStyleSheet(QString("QStatusBar::item{border: 0px}"));

    QLabel *label = new QLabel(QStringLiteral("Copyright 2022 星野家今天的饭 All rights reserved."), this);
    label->setFont(QFont("Segoe Script"));
    ui->statusbar->addPermanentWidget(label);

    setWindowTitle("Program Management System by Today's meal of Hoshino Family");






    QCompleter *completer = new QCompleter();
    completer->setFilterMode(Qt::MatchStartsWith);
    completer->setCompletionMode(QCompleter::PopupCompletion);
    QStringList valueList;
    valueList << "pms music" << "pms sound";








    addAppWidget = nullptr;
    addFileWidget = nullptr;
    soundEffectWidget = nullptr;
    musicConfigWidget = nullptr;
    deploymentConfigWidget = nullptr;
    queryPCInfoWidget = nullptr;


    QFile file(QDir::currentPath() + "/PMS_data/data.corgi");

    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file);
        QString line = in.readLine();
        QStringList strList = line.split("#&#");
        ui->comboBox_left->clear();
        while(!line.isNull()) {
            ui->comboBox_left->addItem(strList[1]);

            valueList << strList[1];


            line = in.readLine();
            strList = line.split("#&#");
        }

        file.close();



    }


    QStringListModel *listModel;
    listModel = new QStringListModel(valueList, this);
    completer->setModel(listModel);
    ui->txtLabel->setCompleter(completer);






    QFile file1(QDir::currentPath() + "/PMS_data/info.corgi");

    if(file1.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QTextStream in(&file1);
        QString line = in.readLine();
        QStringList strList = line.split("#&#");
        ui->comboBox_right->clear();
        while(!line.isNull()) {
            ui->comboBox_right->addItem(strList[1]);
            line = in.readLine();
            strList = line.split("#&#");
        }

        file1.close();



    }


    connect(timer, SIGNAL(timeout()), this, SLOT(getTime_slot()));

    connect(ui->btn_open, SIGNAL(clicked(bool)), this, SLOT(btn_open_slot()));
    connect(ui->btn_close, SIGNAL(clicked(bool)), this, SLOT(btn_close_slot()));
    connect(ui->btn_refresh, SIGNAL(clicked(bool)), this, SLOT(btn_refresh_slot()));
    connect(ui->btn_start, SIGNAL(clicked(bool)), this, SLOT(btn_start_slot()));
    connect(ui->btn_browse, SIGNAL(clicked(bool)), this, SLOT(btn_browse_slot()));



    connect(ui->txtLabel, SIGNAL(returnPressed()), this, SLOT(btn_open_slot()));


    connect(ui->notepad, SIGNAL(triggered(bool)), this, SLOT(act_notepad_slot()));
    connect(ui->calc, SIGNAL(triggered(bool)), this, SLOT(act_calc_slot()));
    connect(ui->mspaint, SIGNAL(triggered(bool)), this, SLOT(act_mspaint_slot()));
    connect(ui->addApps, SIGNAL(triggered(bool)), this, SLOT(act_addapps_slot()));
    connect(ui->addFiles, SIGNAL(triggered(bool)), this, SLOT(act_addfiles_slot()));

    connect(ui->pmsGames, SIGNAL(triggered(bool)), this, SLOT(act_pmsGames_slot()));


    connect(ui->soundEffect, SIGNAL(triggered(bool)), this, SLOT(act_soundEffect_slot()));
    connect(ui->music, SIGNAL(triggered(bool)), this, SLOT(act_music_slot()));
    connect(ui->deployment, SIGNAL(triggered(bool)), this, SLOT(act_deployment_slot()));

    connect(ui->pcInfo, SIGNAL(triggered(bool)), this, SLOT(act_pcInfo_slot()));


}




void MainWindow::getTime_slot() {

    QTime currTime = QTime::currentTime();

    ui->test_lb->setText(currTime.toString());


}

void MainWindow::act_notepad_slot() {

    QProcess *process = new QProcess(this);

    process->startDetached("notepad");

}

void MainWindow::act_calc_slot() {
    QProcess *process = new QProcess(this);

    process->startDetached("calc");
}

void MainWindow::act_mspaint_slot() {
    QProcess *process = new QProcess(this);

    process->startDetached("mspaint");
}

void MainWindow::act_addapps_slot() {

    if(!addAppWidget) addAppWidget = new AddApplication();

    addAppWidget->show();

}

void MainWindow::act_addfiles_slot() {

    if(!addFileWidget) addFileWidget = new AddFile();

    addFileWidget->show();

}

void MainWindow::act_pmsGames_slot() {

    PMSRockPaperScissors *ww = new PMSRockPaperScissors();

    ww->show();

//    PMSGame* pmsGameW = new PMSGame();
//    pmsGameW->show();

}

void MainWindow::act_soundEffect_slot() {

    if(!soundEffectWidget) soundEffectWidget = new SoundEffectConfig();



    if(!soundEffectWidget->isVisible()) {
        Global::playSoundEffect(QString("/change.wav"));
        soundEffectWidget->show();
    }




}

void MainWindow::act_music_slot() {


    if(!musicConfigWidget) musicConfigWidget = new MusicConfig();

    if(!musicConfigWidget->isVisible()) {
        Global::playSoundEffect(QString("/music.wav"));
        musicConfigWidget->show();
    }

}

void MainWindow::act_deployment_slot() {
    if(!deploymentConfigWidget) deploymentConfigWidget = new DeploymentConfig();

    if(!deploymentConfigWidget->isVisible()) {
        //Global::playSoundEffect(QString("/deployment.wav"));
        deploymentConfigWidget->show();
    }
}

void MainWindow::act_pcInfo_slot() {
    if(!queryPCInfoWidget) queryPCInfoWidget = new QueryPCInfo();

    if(!queryPCInfoWidget->isVisible()) {
        //配音
        delete queryPCInfoWidget;
        queryPCInfoWidget = new QueryPCInfo();
        queryPCInfoWidget->show();
    }
}


void MainWindow::btn_open_slot() {




    QString str = ui->txtLabel->text();


    if(!str.length()) {
       QMessageBox::critical(this, "Hint", "Cannot open this program!");
        return;
    }


    if(!QString::compare(str, "pms music")) {

        act_music_slot();


        QTimer::singleShot(0, this, [=]{
            ui->txtLabel->clear();
        });

        //ui->txtLabel->clear();
        return;
    }else if(!QString::compare(str, "pms sound")) {

        act_soundEffect_slot();


        QTimer::singleShot(0, this, [=]{
            ui->txtLabel->clear();
        });


        return;



    }






    QProcess *process = new QProcess(this);


    QFile file(QDir::currentPath() + "/PMS_data/data.corgi");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(this, "Error", QStringLiteral("文件打开失败！"), QStringLiteral("确定"));
        return;
    }

    Global::playSoundEffect(QString("/open.wav"));

    QTextStream in(&file);
    QString line = in.readLine();
    QStringList strList = line.split("#&#");
    QString target = "", PMS_flag = "";


    while(!line.isNull()) {
        if(!QString::compare(str, strList[1])) {target = strList[0]; PMS_flag = strList[2]; break;}
        line = in.readLine();
        strList = line.split("#&#");
    }

    file.close();

    if(target.length()) {
        if(!QString::compare(PMS_flag, QString("PMS-1"))) process->startDetached(target);
        else process->startDetached(target, QStringList(), QString(QFileInfo(target).absolutePath()));
    }
    else process->close();



    QTimer::singleShot(0, this, [=]{
        ui->txtLabel->clear();
    });
    //ui->txtLabel->clear();





}

void MainWindow::btn_close_slot() {



    Global::playSoundEffect("/close.wav");


    if(Global::soundSelect.length()) {
        QEventLoop eventloop;
        QTimer::singleShot(5100, &eventloop, SLOT(quit()));
        eventloop.exec();
    }


    this->close();
}

void MainWindow::btn_refresh_slot() {

    int cnt = 0;


    QFile file(QDir::currentPath() + "/PMS_data/data.corgi");

    if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {

        cnt ++;


        QTextStream in(&file);
        QString line = in.readLine();
        QStringList strList = line.split("#&#");

        ui->comboBox_left->clear();

        while(!line.isNull()) {
            ui->comboBox_left->addItem(strList[1]);
            line = in.readLine();
            strList = line.split("#&#");
        }

        file.close();
    }

    QFile file1(QDir::currentPath() + "/PMS_data/info.corgi");

    if(file1.open(QIODevice::ReadOnly | QIODevice::Text)) {

        cnt ++;


        QTextStream in(&file1);
        QString line = in.readLine();
        QStringList strList = line.split("#&#");

        ui->comboBox_right->clear();

        while(!line.isNull()) {
            ui->comboBox_right->addItem(strList[1]);
            line = in.readLine();
            strList = line.split("#&#");
        }

        file1.close();
    }

    if(ui->txtLabel->text().length())
        ui->txtLabel->clear(), cnt ++;


    if(cnt) Global::playSoundEffect(QString("/refresh.wav"));

}


void MainWindow::btn_start_slot() {


    QString str = ui->comboBox_left->currentText();


    if(!str.length()) {
        QMessageBox::critical(this, "Error", "Cannot start an empty program!");
        return;
    }

    QProcess *process = new QProcess(this);


    QFile file(QDir::currentPath() + "/PMS_data/data.corgi");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(this, "Error", QStringLiteral("文件打开失败！"), QStringLiteral("确定"));
        return;
    }

    Global::playSoundEffect(QString("/start.wav"));

    QTextStream in(&file);
    QString line = in.readLine();
    QStringList strList = line.split("#&#");
    QString target = "", PMS_flag = "";


    while(!line.isNull()) {
        if(!QString::compare(str, strList[1])) {target = strList[0]; PMS_flag = strList[2]; break;}
        line = in.readLine();
        strList = line.split("#&#");
    }

    file.close();
    qDebug() << QString(QFileInfo(target).absolutePath());


    if(target.length()) {
        if(!QString::compare(PMS_flag, QString("PMS-1"))) process->startDetached(target);
        else process->startDetached(target, QStringList(), QString(QFileInfo(target).absolutePath()));
    }
    else process->close();
}

void MainWindow::btn_browse_slot() {


    QString str = ui->comboBox_right->currentText();


    if(!str.length()) {
        QMessageBox::critical(this, "Error", "Cannot browse an empty file!");
        return;
    }


    QFile file(QDir::currentPath() + "/PMS_data/info.corgi");

    if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
        QMessageBox::critical(this, "Error", QStringLiteral("文件打开失败！"), QStringLiteral("确定"));
        return;
    }

    Global::playSoundEffect(QString("/browse.wav"));

    QTextStream in(&file);
    QString line = in.readLine();
    QStringList strList = line.split("#&#");
    QString target = "";





    while(!line.isNull()) {
        if(!QString::compare(str, strList[1])) {target = strList[0]; break;}
        line = in.readLine();
        strList = line.split("#&#");
    }

    file.close();
    //qDebug() << QString(QFileInfo(target).absolutePath());


    if(target.length()) QDesktopServices::openUrl(QUrl(target));
}


MainWindow::~MainWindow()
{
    delete ui;
}

